﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Channels;

using SilverlightNotepad.SilverlightNotepadWebService;
using System.Text;

namespace SilverlightNotepad
{
    public partial class Page : UserControl, ImportContent
    {
        public int intPortalID;
        public int intModuleId;
        public int intUserID;
        public string strPassword;
        public string strWebServiceBase;

        public Page()
        {
            // Required to initialize variables
            InitializeComponent();
            Loaded += new RoutedEventHandler(Page_Loaded);
        }

        #region Page_Loaded
        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            // Get parameters from the Tag
            char[] delimiterChars = { ',' };
            string[] strParameters = this.Tag.ToString().Split(delimiterChars);

            intPortalID = Convert.ToInt32(strParameters[0]);
            intModuleId = Convert.ToInt32(strParameters[1]);
            intUserID = Convert.ToInt32(strParameters[2]);
            strPassword = strParameters[3];
            strWebServiceBase = strParameters[4];

            ShowSilverlightNotepad();

            this.btnSave.Click += new RoutedEventHandler(btnSave_Click);
        }
        #endregion

        #region ShowSilverlightNotepad
        private void ShowSilverlightNotepad()
        {
            BasicHttpBinding bind = new BasicHttpBinding();
            EndpointAddress MyEndpointAddress = new EndpointAddress(strWebServiceBase + "SilverlightNotepadWebService.asmx");
            var proxy = new SilverlightNotepadWebServiceSoapClient(bind, MyEndpointAddress);

            proxy.GetNotepadContentsCompleted += new EventHandler<GetNotepadContentsCompletedEventArgs>(proxy_GetNotepadContentsCompleted);
            proxy.GetNotepadContentsAsync(intPortalID, intModuleId, intUserID, strPassword);
        }

        void proxy_GetNotepadContentsCompleted(object sender, GetNotepadContentsCompletedEventArgs e)
        {
            this.txtContents.Text = (string)e.Result;
        }
        #endregion

        #region SaveNotepad
        void btnSave_Click(object sender, RoutedEventArgs e)
        {
            BasicHttpBinding bind = new BasicHttpBinding();
            EndpointAddress MyEndpointAddress = new EndpointAddress(strWebServiceBase + "SilverlightNotepadWebService.asmx");
            var proxy = new SilverlightNotepadWebServiceSoapClient(bind, MyEndpointAddress);

            proxy.SaveNotepadContentsAsync(intPortalID, intModuleId, intUserID, strPassword, this.txtContents.Text);
            SaveAnimation.Begin();
        }
        #endregion

        #region ImportContent
        public bool ImportContent(FrameworkElement objFrameworkElement)
        {
            // This import method will only import text content contained
            // in TextBlocks that are placed on a Canvas

            StringBuilder StringBuilder = new StringBuilder();
            TextBlock objTextBlock = objFrameworkElement as TextBlock;

            // If the element being imported is not a TextBlock return false
            if (objTextBlock == null)
            {
                return false;
            }

            try
            {
                // Add the contents of the TextBlock to the output
                StringBuilder.Append(String.Format(" {0}", objTextBlock.Text));
            }
            catch
            {
                return false;
            }

            // Was content in the output ?
            if (StringBuilder.Length > 0)
            {
                // Add output to the Textbox display
                this.txtContents.Text = String.Format("{0} {1}", this.txtContents.Text, StringBuilder.ToString());
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
    }
}